package com.example.forjava.write;

import org.apache.commons.lang3.time.StopWatch;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class FastFileWriter {
    public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch();
        // 文件路径
        String filePath = "D:\\output.txt";
        // 要写入的行数
        int totalLines = 300000;

        // 设置缓冲区大小，这里设置为16KB，可以根据实际情况调整
        int bufferSize = 16384; // 16 * 1024
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath), bufferSize)) {
            long startTime = System.currentTimeMillis();
            stopWatch.start();
            for (int i = 0; i < totalLines; i++) {
                // 这里模拟一行数据，实际应用中替换为真实数据
                String line = "docker run -d --hostname=rabbitmqhost -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -e RABBITMQ_DEFAULT_VHOST=my_vhost -vdocker run -d --hostname=rabbitmqhost -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -e RABBITMQ_DEFAULT_VHOST=my_vhost -v  " + i + "\n";
                writer.write(line);
            }
            stopWatch.stop();
            long endTime = System.currentTimeMillis();
            System.out.println("写入完成，耗时：" + (endTime - startTime) + " 毫秒");
            System.out.println("写入完成，耗时：" + stopWatch.formatTime() + " 毫秒");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}